﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.Web.UserControls;
using BEGDC.CC.BL.CCFlow.CR;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.Common;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web.Common
{
	/// <summary>
	/// Summary description for AccompanyInput.
	/// </summary>
	public class AccompanyInputForCV : System.Web.UI.Page
	{
		#region Variables
		protected System.Web.UI.WebControls.TextBox tbTitle;
		protected System.Web.UI.WebControls.Button Btn_Save;
		protected System.Web.UI.WebControls.Button Btn_Close;
		protected BEGDC.CC.Web.Control.UserSearchControl Ctl_AttendeeSearch;
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			string AccompanyStr = Request["AccompanyID"];

			if(AccompanyStr!=null)
			{
				this.AccompanyID = Convert.ToInt32(AccompanyStr);
			}
			else
			{
				this.AccompanyID = 0;
			}

			if(!this.IsPostBack)
			{
				this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";
				this.Btn_Close.Attributes["onclick"] = "javascript:OnClose();";

				if(this.AccompanyID>0)
				{
					InitForm();
				}
			}
			this.Ctl_AttendeeSearch.TitleControlName = this.tbTitle.ClientID;
		}

		#region AccompanyID
		protected int AccompanyID
		{
			get{ return (int)this.ViewState["AccompanyID"]; }
			set{ this.ViewState["AccompanyID"] = value; }
		}
		#endregion

		#region ObtainAttendeeFromDataTable
		private DataRow ObtainAttendeeFromDataTable(DataTable datatable)
		{
			if(datatable==null||datatable.Rows.Count==0||this.AccompanyID==0) return null;

			string IdStr = null;
			for(int i=0;i<datatable.Rows.Count;i++)
			{
				IdStr = datatable.Rows[i]["Id"].ToString();
				int IdInt = Convert.ToInt32(IdStr);
				if(IdInt==this.AccompanyID)
				{
					return datatable.Rows[i];
				}
			}
			return null;
		}
		#endregion

		#region InitForm
		private void InitForm()
		{
			DataRow row = ObtainAttendeeFromDataTable((DataTable)Session[SessionFlag.AccompanySessionEZ]);
			if(row != null)
			{
				this.Ctl_AttendeeSearch.EmployeeID = (row["EmployeeId"].ToString());
				this.Ctl_AttendeeSearch.EmployeeName = row["Name"].ToString();
				this.tbTitle.Text = row["Title"].ToString();
			}
		}
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Btn_Save.Click += new EventHandler(this.Btn_Save_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region Update Accompany Session
		private void UpdateAccompanySession(out bool isExisted)
		{
			isExisted = false;
			DataTable dataTable = null;
			DataRow dataRow = null;
			object SessAccompanyAttendee = Session[SessionFlag.AccompanySessionEZ];
			if(SessAccompanyAttendee!=null)
			{
				dataTable = (DataTable)SessAccompanyAttendee;
			}
			else
			{
				Response.Write("<script>alert('The session is missed, please reopen the application!!')</script>");
				return;
			}
			
			if(this.AccompanyID == 0)
			{
				//Added by joe 2007/6/22
				string id = this.Ctl_AttendeeSearch.EmployeeID;
				foreach( DataRow row in dataTable.Rows )
				{
					if( row["EmployeeId"].ToString() == id )						
					{
						isExisted = true;
						Response.Write("<script>alert('This person is already in the list!')</script>");
						return;
					}

				}
				dataRow = dataTable.NewRow();

				dataRow["EmployeeId"] = this.Ctl_AttendeeSearch.EmployeeID.ToString();
				dataRow["Name"] = this.Ctl_AttendeeSearch.EmployeeName;
				dataRow["Title"] = this.tbTitle.Text;

				dataTable.Rows.Add(dataRow);
			}
			else if(this.AccompanyID > 0)
			{
				dataRow = dataTable.Rows[this.AccompanyID-1];

				dataRow["EmployeeId"] = this.Ctl_AttendeeSearch.EmployeeID.ToString();
				dataRow["Name"] = this.Ctl_AttendeeSearch.EmployeeName;
				dataRow["Title"] = this.tbTitle.Text;
			}
			else
			{
				return;
			}
			
			Session[SessionFlag.AccompanySessionEZ] = SetRowID(dataTable);
		}
		#endregion

		#region Save
		private void Btn_Save_Click(object sender, EventArgs e)
		{
			try
			{
				bool isExisted = false;
				UpdateAccompanySession(out isExisted);
				if( !isExisted )
				{
					Response.Write("<script>window.close();window.returnValue='reload';</script>");
				}
			}
			catch(Exception exp)
			{
				throw exp;
			}
			
		}
		#endregion

		private DataTable SetRowID(DataTable table)
		{
			if(table != null && table.Rows.Count >0)
				for(int i=1; i<=table.Rows.Count; i++)
				{
					table.Rows[i-1][0] = i.ToString();
				}
			return table;
		}
	}
}
